'use strict';

// constants 与 actions 在一起
import { ajax, nameSpace} from 'utils/index';

let ns = nameSpace('RING_TAGS');
export const GET_TAG_TASK = ns('GET_TAG_TASK');
export const REMOVE_TAG_TASK = ns('REMOVE_TAG_TASK');
export const LOAD_START = ns('LOAD_START');
export const LOAD_ERR = ns('LOAD_ERR');
export const CLOSE_DIALOG = ns('CLOSE_DIALOG');
export function closeDialog(){
	return {type: CLOSE_DIALOG};
}
export function removeTagTask(task_id,tag_id) {	
	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START
		});
		ajax({
			  api: 'removeTag',
			  method: 'GET',
			  data:{tag_id}
			},(json) => {
				  //成功后
				ajax({
					  api: 'removeTagTask',
					  method: 'GET',
					  data:{task_id}
					},(json) => {
						  //成功后
						  dispatch({
							  type:REMOVE_TAG_TASK,
							  task_id
						  });
					},(json) => {
						  //失败后
						  //(json.code==53) W2超时
						  const login = (json.code==210);
						  dispatch({
							type: LOAD_ERR,
							message:json.msg+(json.sub_msg?json.sub_msg:""),
							loginFalse:login
							});
					});
			},(json) => {
				  //失败后
				if(json.sub_code=="isv.invalid-tag"){
					//标签失效，仍然删除
					ajax({
						  api: 'removeTagTask',
						  method: 'GET',
						  data:{task_id}
						},(json) => {
							  //成功后
							  dispatch({
								  type:REMOVE_TAG_TASK,
								  task_id
							  });
						},(json) => {
							  //失败后
							  //(json.code==53) W2超时
							  const login = (json.code==210);
							  dispatch({
								type: LOAD_ERR,
								message:json.msg+(json.sub_msg?json.sub_msg:""),
								loginFalse:login
								});
						});
				}
				  //(json.code==53) W2超时
				  const login = (json.code==210);
				  dispatch({
					type: LOAD_ERR,
					message:json.msg+(json.sub_msg?json.sub_msg:""),
					loginFalse:login
					});
			});
		
	}
}
export function getTagTask(params) {	
	return (dispatch) => {
		//发送请求前
		dispatch({
		  type: LOAD_START
		});
		ajax({
		  api: 'getTagTask',
		  method: 'GET',
		  data:params
		},(json) => {
			  //成功后
			  dispatch({
				  type:GET_TAG_TASK,
				  message:json.msg,
				  data:json.data,
				  total:json.total
			  });
		},(json) => {
			  //失败后
			  //(json.code==53) W2超时
			  const login = (json.code==210);
			  dispatch({
				type: LOAD_ERR,
				message:json.msg+(json.sub_msg?json.sub_msg:""),
				loginFalse:login
				});
		});
	}
}



